Omplus.base.define(Omplus.models.Model, {
    omplusName:"UsersSearch",
    controller:"userController",
    value:{
        "username":{
            omplusType:"String"
        },
        delta:{
            omplusType:"Number",
            value:5
        },
        start:{
            omplusType:"Number",
            value:0
        }
    }
});

Omplus.models.UsersSearchModelProto = Omplus.base.extend(Omplus.models.Object,
    {
        omplusType:"UsersSearchModelProto",
        value:{
            "name":{
                omplusType:"String"
            },
            "last_name":{
                omplusType:"String"
            },
            "id":{
                omplusType:"Number"
            },
            "username":{
                omplusType:"String"
            },
            "privilege_name":{
                omplusType:"String"
            },
            "kontrolor":{
                omplusType:"String"
            }
        }
    });

Omplus.base.define(Omplus.models.Model, {
    omplusName:"UsersSearchResults",
    controller:"userController",
    value:{
        users:{
            omplusType:"Array",
            omplusProto:"UsersSearchModelProto"
        },
        total:{
            omplusType:"Number"
        }
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"EditUser",
    controller:"userController",
    value:{
        privilege_id:{
            omplusType:"Number"
        },
        "password":{
            omplusType:"String",
            nullable:false
        },
        "confirm_password":{
            omplusType:"String",
            validateWith:"EditUser.password",
            nullable:false
        },
        "id":{
            omplusType:"Number",
            synchroniseWith:"userChooser.current.id"
        }
    },
    actions:[
        {
            omplusType:"Get",
            omplusName:"editPassword",
            proxy:{
                omplusType:"AjaxProxy"
            },
            responseModel:"EditUser",
            requestModel:"EditUser"
        }
    ]
});

Omplus.models.UserProto = Omplus.base.extend(Omplus.models.Model, {
    omplusType:"UserProto",
    value:{
        "name":{
            omplusType:"String"
        },
        "last_name":{
            omplusType:"String"
        },
        "username":{
            omplusType:"String"
        },
        "privilege_name":{
            omplusType:"String"
        },
        "privilege_id":{
            omplusType:"Number"
        },
        "id":{
            omplusType:"Number"
        }
    }
});

Omplus.base.define(Omplus.models.SingleChooser, {
    omplusName:"userChooser",
    controller:"userController",
    omplusProto:"UserProto"
});

Omplus.views.OpeningUsersTabView = Omplus.base.extend(
    Omplus.views.OpeningTabView, {
        omplusType:"OpeningUsersTabView",
        layout:{
            omplusType:"AccordionLayout"
        },
        controller:"userController",
        components:[
            {
                omplusName:"userDetailsFirstPanel",
                omplusType:"AccordionPanel",
                components:[
                    {
                        model:"userChooser.current.id",
                        labelModel:"Assets.list.user_id_label.localValue",
                        inputType:"Label"
                    },
                    {
                        model:"userChooser.current.name",
                        labelModel:"Assets.list.name_label.localValue",
                        inputType:"Label"
                    },
                    {
                        model:"userChooser.current.last_name",
                        labelModel:"Assets.list.last_name_label.localValue",
                        inputType:"Label"
                    },
                    {
                        model:"userChooser.current.username",
                        labelModel:"Assets.list.username_label.localValue",
                        inputType:"Label"
                    },
                    {
                        model:"userChooser.current.privilege_name",
                        labelModel:"Assets.list.privileges_label.localValue",
                        inputType:"Label"
                    }
                ],
                controller:"languageController",
                model:"Assets.list.user_details_label.localValue"
            },
            {
                omplusName:"userDetailsSecondPanel",
                omplusType:"AccordionPanel",
                components:[
                    {
                        model:"EditUser.password",
                        labelModel:"Assets.list.label_new_password.localValue",
                        inputType:"PasswordInput",
        				inputCssClasses:["no-transform"]
                    },
                    {
                        model:"EditUser.confirm_password",
                        labelModel:"Assets.list.label_confirm_new_password.localValue",
                        inputType:"PasswordInput",
        				inputCssClasses:["no-transform"]
                    },
                    {
                        omplusType:"Button",
                        omplusName:"editUsersPasswords",
                        controller:"languageController",
                        model:"Assets.list.label_save_password.localValue",
                        cssClasses:[ "search-button" ],
                        width:"300px",
                        click : function(){
                            Omplus.globals.getObject("EditUser").getAction("editPassword").run();
                        }
                    }
                ],
                controller:"languageController",
                model:"Assets.list.label_change_password.localValue"
            }
        ]
    });

Omplus.base.define(Omplus.models.Model, {
    omplusName:"NewUser",
    controller:"userController",
    value:{
        password:{
            omplusType:"String",
            nullable:false
        },
        confirm_password:{
            omplusType:"String",
            validateWith:"NewUser.password",
            nullable:false
        },
        username:{
            omplusType:"String",
            nullable:false,
            minimum:6
        },
        name:{
            omplusType:"String",
            nullable:false
        },
        last_name:{
            omplusType:"String",
            nullable:false
        },
        privilege_id:{
            omplusType:"Number",
            nullable:false,
            synchroniseWith:"Privileges.forUsers.id"
        }
    },
    actions:[
        {
            omplusType:"Get",
            omplusName:"register",
            proxy:{
                omplusType:"AjaxProxy"
            },
            responseModel:"NewUser",
            requestModel:"NewUser",
            start:function () {
                Omplus.globals.getObject("addUserOverlayPanel").show();
            },
            callback:function () {
                Omplus.globals.getObject("addUserOverlayPanel").hide();
            }
        }
    ]
});

Omplus.views.AddUserError = Omplus.base.extend(Omplus.views.TableRow, {
    omplusType:"AddUserError",
    components:[
        {
            omplusName:"addUserErrorValue",
            omplusType:"Label",
            model:"localValue"
        }
    ]
});

Omplus.base
    .define(
    Omplus.views.ManagementPanel,
    {
        omplusName:"usersPanel",
        controller:"userController",
        renderTo:"managements",
        models:{
            chooser:"userChooser",
            search:"UsersSearch",
            searchResult:"UsersSearchResults",
            searchResultList:"users",
            forDisplay:"username"
        },
        views:{
            chooser:{
                title:"Assets.list.users_label.localValue",
                components:[
                    {
                        omplusName:"usersTabs",
                        omplusType:"OpeningTabPanel",
                        omplusProto:"OpeningUsersTabView",
                        controller:"userController",
                        model:"userChooser",
                        showModel:"username",
                        onChange:function(){
                            Omplus.globals.getObject("EditUser.password").setValue(null);
                            Omplus.globals.getObject("EditUser.confirm_password").setValue(null);
                        }
                    }
                ]
            },
            search:[
                {
                    model:"UsersSearch.username",
                    labelModel:"Assets.list.username_label.localValue",
    				inputCssClasses:["no-transform"]
                },
                {
                    // Per page
                    inputType:"Select",
                    model:"PerPages",
                    showModel:"number",
                    current:"forUsers",
                    controller:"languageController",
                    labelModel:"Assets.list.label_per_page.localValue",
                    perPage:true
                },
                {
                    // Search button
                    inputType:"Button",
                    controller:"languageController",
                    model:"Assets.list.label_search_button.localValue",
                    cssClasses:[ "search-button" ],
                    labelWidth:"0%",
                    inputWidth:"100%",
                    searchButton:true
                }
            ],
            searchResult:[
                {
                    labelModel:"Assets.list.user_id_label.localValue",
                    width:"100px",
                    omplusType:"Label",
                    model:"id"
                },
                {
                    labelModel:"Assets.list.username_label.localValue",
                    width:"150px",
                    omplusType:"Label",
                    model:"username"
                },
                {
                    labelModel:"Assets.list.name_label.localValue",
                    width:"150px",
                    omplusType:"Label",
                    model:"name"
                },
                {
                    labelModel:"Assets.list.last_name_label.localValue",
                    width:"150px",
                    omplusType:"Label",
                    model:"last_name"
                },
                {
                    labelModel:"Assets.list.privileges_label.localValue",
                    width:"150px",
                    omplusType:"Label",
                    model:"privilege_name"
                },
                {
                    omplusType:"Button",
                    model:"Assets.list.details_label.localValue",
                    width:"200px",
                    controller:"languageController",
                    getDetails:true,
                    errorable:false
                }
            ]
        },
        components:[
            {
                omplusType:"TabPanel",
                omplusName:"addUserPanel",
                layout:{
                    omplusType:"HorizontalLayout"
                },
                controller:"languageController",
                model:"Assets.list.new_label.localValue",
                components:[
                    {
                        omplusType:"Panel",
                        omplusName:"addUserleftPanel",
                        layout:{
                            omplusType:"VerticalLayout"
                        },
                        width:"300px",
                        components:[
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserUsername",
                                labelModel:"Assets.list.username_label.localValue",
                                labelController:"languageController",
                                model:"NewUser.username",
                                controller:"userController",
                				inputCssClasses:["no-transform"]
                            },
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserPassword",
                                labelModel:"Assets.list.label_password.localValue",
                                labelController:"languageController",
                                inputType:"PasswordInput",
                                model:"NewUser.password",
                                controller:"userController",
                				inputCssClasses:["no-transform"]
                            },
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserConfirmPassword",
                                labelModel:"Assets.list.label_confirm_password.localValue",
                                labelController:"languageController",
                                inputType:"PasswordInput",
                                model:"NewUser.confirm_password",
                                controller:"userController",
                				inputCssClasses:["no-transform"]
                            }
                        ]
                    },
                    {
                        omplusType:"Panel",
                        omplusName:"addUserMiddlePanel",
                        layout:{
                            omplusType:"VerticalLayout"
                        },
                        width:"300px",
                        components:[
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserPrivilege",
                                labelModel:"Assets.list.privileges_label.localValue",
                                labelController:"languageController",
                                inputType:"Select",
                                model:"Privileges",
                                current:"forUsers",
                                showModel:"name",
                                controller:"languageController"
                            },
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserName",
                                labelModel:"Assets.list.name_label.localValue",
                                labelController:"languageController",
                                model:"NewUser.name",
                                controller:"userController",
                				inputCssClasses:["no-transform"]
                            },
                            {
                                omplusType:"LabelInputPair",
                                omplusName:"addUserLastname",
                                labelModel:"Assets.list.last_name_label.localValue",
                                labelController:"languageController",
                                model:"NewUser.last_name",
                                controller:"userController",
                				inputCssClasses:["no-transform"]
                            }
                        ]
                    },
                    {
                        omplusType:"Panel",
                        omplusName:"addUserRightPanel",
                        layout:{
                            omplusType:"VerticalLayout"
                        },
                        width:"300px",
                        components:[
                            {
                                omplusType:"Button",
                                omplusName:"addUser",
                                model:"Assets.list.add_label.localValue",
                                controller:"languageController",
                                click:function () {
                                    Omplus.globals.getObject("NewUser")
                                        .getAction("register")
                                        .run();
                                }
                            }
                        ]
                    },
                    {
                        omplusType:"Panel",
                        omplusName:"addUserOverlayPanel",
                        renderTo:"addUserPanel",
                        width:"900px",
                        showEffect:{
                            name:"fade",
                            time:1
                        },
                        hideEffect:{
                            name:"fade",
                            time:1
                        }
                    }
                ]
            }
        ]
    });
